package com.starblink.login.loginemail.ui

import android.app.Activity
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.ext.isEmail
import com.starblink.android.basic.ext.toast
import com.starblink.android.common.utils.PasswordUtils
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.basic.util.ActivityStackManage
import com.starblink.basic.util.CommUtils
import com.starblink.rocketreserver.FetchAccountInfoQuery
import com.starblink.rocketreserver.LoginAccountMutation
import com.starblink.android.basic.bridge.BridgeCommand
import com.starblink.android.basic.bridge.BridgeCommandHandler
import com.starblink.android.basic.bridge.BridgeMessage
import com.starblink.android.basic.bridge.BridgeUtil
import kotlinx.coroutines.launch

class LoginEmailVM : BaseViewModel() {


//    var isAgreePolicy: Boolean = true


    fun loginBtn(input: String, pass: String) {

        if (input.isEmpty()) {
            toast(CommUtils.getString(com.starblink.basic.style.R.string.account_input_warming))
            return
        }

        if (pass.isEmpty()) {
            toast(CommUtils.getString(com.starblink.basic.style.R.string.pass_input_warming))
            return
        }

//        if (!isAgreePolicy) {
//            toast(CommUtils.getString(com.starblink.basic.style.R.string.accept_policy))
//            return
//        }


        var isEmail = input.isEmail()
        viewModelScope.launch {
            val response = SkGqlClient.skMutation(
                if (isEmail) LoginAccountMutation(
                    email = Optional.present(input),
                    password = PasswordUtils.generatePassMD5(pass)
                ) else LoginAccountMutation(
                    username = Optional.present(input),
                    password = PasswordUtils.generatePassMD5(pass)
                ),
                showDefaultLoading = true
            )
            if (response?.data != null && response.data!!.loginAccount != null) {
                UserDataCenter.putToken(response.data!!.loginAccount!!)
                val responseUserInfo = SkGqlClient.skQuery(
                    FetchAccountInfoQuery(
                    ),
                    showDefaultLoading = true
                )
                if (responseUserInfo?.data != null && responseUserInfo!!.data!!.fetchAccountInfo != null) {
                    UserDataCenter.setAccountInfo(responseUserInfo!!.data!!.fetchAccountInfo!!)
                    toast(com.starblink.basic.style.R.string.login_success)
                    //刷新用户数据
                    FlowBus.boolFlowBusNotify(FlowConst.LOGIN_STATUS_CHANGE)
                    //登录状态 主进程向web进程发送通知
                    BridgeUtil.sendFlowBus(FlowConst.LOGIN_STATUS_CHANGE)

                    ActivityStackManage.currentActivity().setResult(Activity.RESULT_OK)
                    ActivityStackManage.currentActivity().finish()
                } else {
                    UserDataCenter.clearToken()
                }


            }
        }
    }

}